package cassys.actions.assessmentGroupManagement;

import cassys.application.DTOs.AssessmentGroupDTO;

import com.opensymphony.xwork2.ActionSupport;
import cassys.domain.classManagement.*;
import cassys.ejb.clients.AssessmentManagementDelegate;
import com.opensymphony.xwork2.ModelDriven;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author User
 */
public class ViewAssessmentGroup extends ActionSupport implements ModelDriven<AssessmentGroupDTO> {

    private AssessmentManagementDelegate assessmentDelegate;
    private AssessmentGroupDTO groupDTO = new AssessmentGroupDTO();
    private String [] checked;

    public ViewAssessmentGroup() {
        assessmentDelegate = new AssessmentManagementDelegate();
    }

    @Override
    public String execute() throws Exception {
        System.out.println("module view action started");
        return SUCCESS;
    }

    public String updateAssessmentGroup() throws Exception {
        long  [] ids = new long[checked.length];
        for(int i=0;i < checked.length;i++){
          ids[i] = Long.valueOf(checked[i]);
        }
        groupDTO.setMembers(ids);
        assessmentDelegate.editAssessmentGroup(groupDTO);
        return SUCCESS;
    }

    public String deleteAssessmentGroup() throws Exception {
        
        assessmentDelegate.deleteAssessmentGroup(groupDTO);
        return SUCCESS;
    }
    
    public List<Student> getStudents(){
        List<Student> result = new ArrayList<Student>();
        List<Student> checkedStudents = assessmentDelegate.getUngroupedStudent(groupDTO.getAssessmentId());
        for(Student checkedStudent: checkedStudents){
            Student student = checkedStudent;
            student.setSelected(true);
            result.add(student);
        }
        result.addAll(assessmentDelegate.getUngroupedStudent(groupDTO.getAssessmentId()));
        return result;
    }

    public String[] getChecked() {
        return checked;
    }

    public void setChecked(String[] checked) {
        this.checked = checked;
    }
    
    

   

    @Override
    public AssessmentGroupDTO getModel() {
        return this.groupDTO;
    }
}
